/**
 * 〈〉
 *
 * @author 王青杰
 * @create 2018-08-0810:20
 * @since 1.0.0
 */
package org.wlgzs.website.wlgzsoffice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.wlgzs.website.wlgzsoffice.dao.CriteriaDao;
import org.wlgzs.website.wlgzsoffice.entity.TbCriteria;
import org.wlgzs.website.wlgzsoffice.enums.ResultEnum;
import org.wlgzs.website.wlgzsoffice.service.CriteriaService;
import org.wlgzs.website.wlgzsoffice.util.ResultMsg;
import java.util.*;
import javax.annotation.Resource;

@Service
public class CriteriaServiceImpl  implements CriteriaService{
    @Resource
    CriteriaDao criteriaDao;

    @Override
    public ResultMsg insert(TbCriteria criteria) {
        if(criteria!=null){
            criteriaDao.insert(criteria);
            return new ResultMsg(ResultEnum.SAVE,criteria);
        }
        return new ResultMsg(ResultEnum.UNSAVE);
    }

    @Override
    public ResultMsg delete(int criteriaId) {
        if(criteriaDao.selectByPrimaryKey(criteriaId)!=null){
            criteriaDao.deleteByPrimaryKey(criteriaId);
            PageHelper.startPage(0,10);
            List<TbCriteria> criteriaList  = criteriaDao.checkAll();
            PageInfo pageInfo = new PageInfo(criteriaList);
            return new ResultMsg(ResultEnum.DELETE,pageInfo);
        }
        PageHelper.startPage(0,10);
        List<TbCriteria> criteriaList  = criteriaDao.checkAll();
        PageInfo pageInfo = new PageInfo(criteriaList);
        return new ResultMsg(ResultEnum.UNDELETE,pageInfo);
    }

    @Override
    public ResultMsg update(TbCriteria criteria) {
        if(criteriaDao.selectByPrimaryKey(criteria.getCriteriaId())!=null){
            criteriaDao.updateByPrimaryKey(criteria);
            return new ResultMsg(ResultEnum.UPDATE,criteriaDao.selectByPrimaryKey(criteria.getCriteriaId()));
        }
        return new ResultMsg(ResultEnum.UNUPDATE,criteria);
    }

    @Override
    public ResultMsg checkByKey(int pageNum, TbCriteria criteria) {
        if(criteria.getCriteriaId()==null){
            criteria.setCriteriaId(0);
        }
        if(criteria.getAssessCriteria()==null){
            criteria.setAssessCriteria("");
        }
        PageHelper.startPage(pageNum,10);
        List<TbCriteria> criteriaList = criteriaDao.checkBykey(criteria.getCriteriaId(),criteria.getAssessCriteria());
        PageInfo pageInfo = new PageInfo(criteriaList);
        return new ResultMsg(ResultEnum.QUIRY,pageInfo);
    }

    @Override
    public ResultMsg getAllCriteria(int pageNum) {
        PageHelper.startPage(pageNum,10);
        List<TbCriteria> criteriaList = criteriaDao.checkAll();
        PageInfo pageInfo = new PageInfo(criteriaList);
        return new ResultMsg(ResultEnum.QUIRY,pageInfo);
    }
}
